<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="时间选择" prop="endTime">
        <el-date-picker
          v-model="queryParams.endTime"
          type="daterange"
          align="right"
          unlink-panels
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="拼团状态" prop="status">
        <el-select v-model="queryParams.status">
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:buying:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:buying:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:buying:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:buying:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->

    <el-table v-loading="loading" :data="buyingList" @selection-change="handleSelectionChange">
      <el-table-column label="头像" align="center" width="60">
        <template v-slot="scope">
          <el-image
            :src="scope.row.avatar"
            fit="fill"
            width="60%">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="开团团长" align="center" width="130">
        <template slot-scope="scope">
          {{ scope.row.nickname }} / {{ scope.row.uid }}
        </template>
      </el-table-column>
      <el-table-column label="开团时间" align="center" prop="endTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="拼团商品" align="center" width="250">
        <template slot-scope="scope">
          {{ scope.row.groupName }} / {{ scope.row.goodsGroupId }}
        </template>
      </el-table-column>
      <el-table-column label="几人团" align="center" prop="buyingCountNum" />
      <el-table-column label="几人参加" align="center" prop="yetBuyingNum" />
      <el-table-column label="结束时间" align="center" prop="endTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_buying_status" :value="scope.row.status"/>
        </template>
      </el-table-column>>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['marketing:combina:details']"
          >查看详情</el-button>
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:buying:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改拼团活动对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-table v-loading="loading"  :data="groupUserList">
        <el-table-column label="用户名称" align="center" width="130">
          <template slot-scope="scope">
            {{ scope.row.nickname }} / {{ scope.row.uid }}
          </template>
        </el-table-column>
        <el-table-column label="用户头像" align="center" width="80">
          <template v-slot="scope">
            <el-image
              :src="scope.row.avatar"
              fit="fill"
              style="width: 50px">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="订单信息" align="center" width="230">
          <template slot-scope="scope">
            {{ scope.row.orderSn }} / {{ scope.row.orderId }}
          </template>
        </el-table-column>
        <el-table-column label="金额" align="center" prop="payPrice" />
        <el-table-column label="订单状态" align="center" prop="status">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_buying_user_status" :value="scope.row.status"/>
          </template>
        </el-table-column>>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { listBuying, getBuying, delBuying, addBuying, updateBuying } from "@/api/marketing/combina";

export default {
  name: "Buying",
  dicts: ['sys_buying_status','sys_buying_user_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 拼团活动表格数据
      buyingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null,
        endTime: null,
      },
      statusOptions: [
        { value: "0", label: '进行中' },
        { value: "1", label: '已完成' },
        { value: "2", label: '未完成' },
      ],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      // 表单参数
      form: {},
      groupUserList:{},
      // 表单校验
      rules: {
        totalPrice: [
          { required: true, message: "商品售价不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询拼团活动列表 */
    getList() {
      this.loading = true;
      listBuying(this.queryParams).then(response => {
        this.buyingList = response.data.data;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        groupBuyingId: null,
        goodsGroupId: null,
        status: null,
        fictiStatus: null,
        fictiNum: null,
        buyingCountNum: null,
        buyingNum: null,
        yetBuyingNum: null,
        delFlag: null,
        endTime: null,
        createTime: null,
        totalPrice: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.groupBuyingId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加拼团活动";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const groupBuyingId = row.groupBuyingId || this.ids
      getBuying(groupBuyingId).then(response => {
        this.groupUserList = response.data;
        this.open = true;
        this.title = "查看详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.groupBuyingId != null) {
            updateBuying(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBuying(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const groupBuyingIds = row.groupBuyingId || this.ids;
      this.$modal.confirm('是否确认删除拼团活动编号为"' + groupBuyingIds + '"的数据项？').then(function() {
        return delBuying(groupBuyingIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/buying/export', {
        ...this.queryParams
      }, `buying_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
